package com.example.demo.test.lecture.algorithm;

public class MoneyCount {
    
    private static int[] count = {5, 2, 2, 3, 5};
    private static int[] value = {1, 5, 10, 50, 100};

    public static void main(String[] args) {
        System.out.println(moneyCountSolve(456));
    }


    public static int moneyCountSolve(int money) {
        int number = 0;
        for (int i = count.length - 1; i >= 0; i--) {
            int c = minInt(money/value[i], count[i]);
            money -= c * value[i];
            number += c;
            System.out.println(value[i] + " need: " + c 
            + " counts.\n The money is: " + money + ". Total is " + number);
        }
        if (money > 0) number = -1;
        return number;
    }

    public static int minInt(int m, int n) {
        if (m < n) {
            return m;
        }
        return n;
    }
}
